package com.ruoyi.web.controller.basic;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.basic.domain.TbPriceHistory;
import com.ruoyi.basic.service.ITbPriceHistoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.Size;

/**
 * 工人工日单价历史数据Controller
 * 
 * @author ruoyi
 * @date 2022-12-05
 */
@Controller
@RequestMapping("/system/history")
public class TbPriceHistoryController extends BaseController
{
    private String prefix = "basic/history";

    @Autowired
    private ITbPriceHistoryService tbPriceHistoryService;

    @RequiresPermissions("system:history:view")
    @GetMapping()
    public String history()
    {
        return prefix + "/history";
    }

    /**
     * 查询工人工日单价历史数据列表
     */
    @RequiresPermissions("system:history:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbPriceHistory tbPriceHistory)
    {
        startPage();
        List<TbPriceHistory> list = tbPriceHistoryService.selectTbPriceHistoryList(tbPriceHistory);
        return getDataTable(list);
    }

    /**
     * 导出工人工日单价历史数据列表
     */
    @RequiresPermissions("system:history:export")
    @Log(title = "工人工日单价历史数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbPriceHistory tbPriceHistory)
    {
        List<TbPriceHistory> list = tbPriceHistoryService.selectTbPriceHistoryList(tbPriceHistory);
        List<TbPriceHistory> collect = list.stream().sorted(Comparator.comparing(TbPriceHistory::getPriceType).thenComparing(TbPriceHistory::getYear)).collect(Collectors.toList());
        ExcelUtil<TbPriceHistory> util = new ExcelUtil<TbPriceHistory>(TbPriceHistory.class);
        return util.exportExcel(collect, "工人工日单价数据");
    }

    /**
     * 新增工人工日单价历史数据
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存工人工日单价历史数据
     */
    @RequiresPermissions("system:history:add")
    @Log(title = "工人工日单价历史数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbPriceHistory tbPriceHistory)
    {
        String userName = getSysUser().getUserName();
        tbPriceHistory.setCreateBy(userName);
        tbPriceHistory.setUpdateBy(userName);
        return toAjax(tbPriceHistoryService.insertTbPriceHistory(tbPriceHistory));
    }

    /**
     * 修改工人工日单价历史数据
     */
    @RequiresPermissions("system:history:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TbPriceHistory tbPriceHistory = tbPriceHistoryService.selectTbPriceHistoryById(id);
        mmap.put("tbPriceHistory", tbPriceHistory);
        return prefix + "/edit";
    }

    /**
     * 修改保存工人工日单价历史数据
     */
    @RequiresPermissions("system:history:edit")
    @Log(title = "工人工日单价历史数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbPriceHistory tbPriceHistory)
    {
        tbPriceHistory.setUpdateBy(getSysUser().getUserName());
        return toAjax(tbPriceHistoryService.updateTbPriceHistory(tbPriceHistory));
    }

    /**
     * 删除工人工日单价历史数据
     */
    @RequiresPermissions("system:history:remove")
    @Log(title = "工人工日单价历史数据", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbPriceHistoryService.deleteTbPriceHistoryByIds(ids));
    }

    // 导入方法
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TbPriceHistory> util = new ExcelUtil<TbPriceHistory>(TbPriceHistory.class);
        List<TbPriceHistory> list = util.importExcel(file.getInputStream());
        String message = tbPriceHistoryService.importData(list, updateSupport,getSysUser().getUserName());
        return AjaxResult.success(message);
    }

    // 下载模板
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<TbPriceHistory> util = new ExcelUtil<TbPriceHistory>(TbPriceHistory.class);
        return util.importTemplateExcel("工人工日单价数据-模板");
    }
}
